// Populate entity data to local storage
var localStore = {

	get: function (key) {
		var result = window.localStorage.getItem(key);
		if (result == null || result == 'null') {
			return null;
		}
		return result;
	},

	getSQL: function (key, callback) {
	},

	save: function (key, data) {
		try {
			window.localStorage.setItem(key, data);
		} catch (e) {}
	},

	removeItem: function (key) {
		window.localStorage.removeItem(key);
	},

	getImage: function (key) {
	},

	saveImage: function (key, data) {
	},

	deleteDirectory: function () {
        var localFileSystem = LocalFileSystem.TEMPORARY;
        // [iOS]
        if (isiOS){
            localFileSystem = LocalFileSystem.PERSISTENT;
        }
		window.requestFileSystem(localFileSystem, 0, function (fileSystem) {
			fileSystem.root.getDirectory(
				strCatchFolder, {
					create: true,
					exclusive: false
				},
				function (entry) {
					entry.removeRecursively(function () {
						console.log("Remove Recursively Succeeded");
					}, localStore.fail);
				}, localStore.fail);
		}, localStore.fail);
	},

	// [iOS]
	moveCacheDirectory: function () {
		window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) {
			window.requestFileSystem(LocalFileSystem.TEMPORARY, 0, function (fileSystem2) {
				fileSystem2.root.getDirectory(
					strCatchFolder, {
						create: false,
						exclusive: false
					},
					function (entry2) {
						entry2.moveTo(fileSystem.root, strCatchFolder, function () { /*success*/ }, function (error) { /*faild*/ });
					}, localStore.fail);
			}, localStore.fail);
		}, localStore.fail);
	},

	saveFile: function (key, data) {
        var localFileSystem = LocalFileSystem.TEMPORARY;
        // [iOS]
        if (isiOS){
            localFileSystem = LocalFileSystem.PERSISTENT;
        }
		window.requestFileSystem(localFileSystem, 5 * 1024 * 1024, function (fileSystem) {
			fileSystem.root.getDirectory(strCatchFolder, {
				create: true,
				exclusive: false
			}, function () {
				fileSystem.root.getFile(strCatchFolder + "/" + key + ".txt", {
					create: true,
					exclusive: false
				}, function (fileEntry) {
					fileEntry.createWriter(function (writer) {
						// [BB]
						if (isBB)
							writer.write(Base64.encode(data));
						else 
							writer.write(data);
					}, localStore.fail);
				}, localStore.fail);
			}, localStore.fail)
		})
	},

	getFile: function (key, callback) {
		setTimeout(function () {
            var localFileSystem = LocalFileSystem.TEMPORARY;
            // [iOS]
            if (isiOS){
                localFileSystem = LocalFileSystem.PERSISTENT;
            }
			window.requestFileSystem(localFileSystem, 5 * 1024 * 1024, function (fileSystem) {
				fileSystem.root.getFile(strCatchFolder + "/" + key + ".txt", null, function (fileEntry) {
					fileEntry.file(function (file) {
						var reader = new FileReader();
						reader.onloadend = function (evt) {
						    // [BB]
						    
							if (isBB)
								callback(Base64.decode(evt.target.result));
							else if (isWP)
							    callback(ko.toJSON(evt.target.result));
                            else
								callback(evt.target.result);
						};
						reader.readAsText(file);
                    }, function(){/* failed */ callback(null);});
                }, function(){/* failed */callback(null);});
            }, function(){/* failed */callback(null);});
		}, 100);
	},

	getAudio: function (key) {
	},

	saveAudio: function (key, data) {
	},

	fail: function (error) {
		console.log(error);
	},

	success: function () {
		console.log('success');
	},

	choosePath: function (result, url) {
		if (url == undefined)
			return;

		var path = url.replace(/\//g, '');
		path = path.replace("http:", '');
		path = path.replace("https:", '');
		path = strCatchFolder + "/" + path;

		// [BB]
		if (isBB) {
			if (localStore.get(path) == 'true') {
			result("file:///accounts/1000/shared/misc/" + path);
			} else {
				result(url);
				localStore.downloadFile(url);
			}
			return;
		}
        var localFileSystem = LocalFileSystem.TEMPORARY;
        // [iOS]
        if (isiOS){
            localFileSystem = LocalFileSystem.PERSISTENT;
        }
		window.requestFileSystem(localFileSystem, 0, function (fileSystem) {
			fileSystem.root.getFile(path, {
				create: false
			}, function (fileEntry) {
				/* file exist, return path */
				result(fileEntry.fullPath);
			}, function () {
				/* file does not exist */
				result(url);
				/* down load file in background */
				localStore.downloadFile(url);
			});
		}, function () {
			/* failed */
			result(url);
		});
	},

	downloadFile: function (url) {
		var path = url.replace(/\//g, '');
		path = path.replace("http:", '');
		path = path.replace("https:", '');
		path = strCatchFolder + "/" + path;

		// [BB]
		if (isBB) {
			try {
				blackberry.io.filetransfer.download(
					url,
					"file:///accounts/1000/shared/misc/" + path,
					function (theFile) {
						localStore.save(path, 'true');
					},
					function (error) {});
			} catch (e) {}
			return;
		}
        var localFileSystem = LocalFileSystem.TEMPORARY;
        // [iOS]
        if (isiOS){
            localFileSystem = LocalFileSystem.PERSISTENT;
        }
		window.requestFileSystem(
			localFileSystem,
			0,
			function onFileSystemSuccess(fileSystem) {
				fileSystem.root.getFile("dummy.html", {
						create: true,
						exclusive: false
					},
					function gotFileEntry(fileEntry) {
						var sPath = fileEntry.fullPath.replace("dummy.html", "");
						var fileTransfer = new FileTransfer();
						fileEntry.remove();
						fileTransfer.download(
							url,
							sPath + path,
							function (theFile) {
							},
							function (error) {
							}
						);
					},
					function () {
					}
				);
			},
			function () {
			}
		);
	}
}
